<template>
	<view>
		<view class="navbar" style="height: 180rpx; padding: 20rpx; display: flex; flex-direction: column;
			justify-content: space-around; align-items: center; background-color: #fff;">
			<text class="title" style="color: #fa3534;font-weight: bold;font-size: 40rpx;">首页</text>
			<view class="search" style="display: flex;flex-direction: row;justify-content: start; width: 95%;
				 border: 2rpx solid #dd6161; border-radius: 50rpx; height: 64rpx; align-items: center; background-color: #f2f2f2;
				 position: relative;">
				<u-icon name="search" style="margin-left: 20rpx;"></u-icon>
				<input type="text" placeholder="请输入关键字" style="font-size: 28rpx; margin-left: 20rpx;" v-model="value">
				<button @click="submit" style="color: white; background-color: #fa3534;position: absolute;
					right: 1rpx; border-radius: 50rpx; height: 95%; width: 120rpx; font-size: 28rpx; display: flex;
					justify-content: center; align-items: center;">搜索</button>
			</view>
		</view>
		<scroll-view scroll-y="true" class="main" style="flex: 1; overflow: hidden;">
			<view class="content" style="background-color: #f7f7f7;padding-top: 0; height: fit-content;">
				<view class="swiper"
					style="background-color: #fff; border-radius: 15rpx; padding: 20rpx;padding-top: 0;">
					<u-swiper :list="swiperList" height="300" @click="to" :current="swiperCurrent"></u-swiper>
				</view>
				<view class="cate">
					<navigator class="cate-item" v-for="item in cateList">
						<image class="icon" :src="item.img"></image>
						<text class="name">{{item.name}}</text>
					</navigator>
					<navigator class="cate-item">
						<u-icon class="icon" name="list-dot" size="80"
							style="border: 6rpx solid #2979ff; border-radius: 50%; text-align: center;display: flex;justify-content: center;align-items: center;"
							color="#2979ff"></u-icon>
						<text class="name">更多服务</text>
					</navigator>
				</view>
				<view class="hot" style="background-color: #fff;padding: 20rpx;">
					<text class="title" style="font-size: 36rpx;color: #303133;">热门主题</text>
					<view class="hot-pannel" style="display: flex;flex-direction: row;justify-content: space-between;
								margin-top: 20rpx;padding: 20rpx 0;border-top: 2rpx solid #dcdfe6; border-bottom: 2rpx solid #dcdfe6;">
						<navigator :url="`/pages/news-detail/news-detail?id=${item.id}`" class="hot-item"
							v-for="item in hotList" style="display: flex;flex-direction: column;flex: 1;
									justify-content: center; align-items: center; height: fit-content">
							<image :src="$baseUrl + item.cover" mode=""
								style="width: 300rpx;height: 280rpx;border-radius: 20rpx;"></image>
							<text class="name" style="margin-top: 20rpx;display: -webkit-box;-webkit-box-orient: vertical;-webkit-line-clamp: 2;
										width: 300rpx;overflow: hidden;">{{item.title}}</text>
						</navigator>
					</view>
				</view>
				<view class="new-pannel" style="margin-top: 20rpx;background-color: #fff;" v-if="flag">
					<u-sticky offset-top="90">
						<u-tabs :list="tabList" :is-scroll="true" :current="tabCurrent" @change="tabChange"></u-tabs>
					</u-sticky>
					<view class="news-list" style="display: flex;flex-direction: column;padding: 20rpx;">
						<view class="news-item"
							style="display: flex;flex-direction: row;justify-content: space-between;padding: 20rpx 0;border-top: 2rpx solid #e4e7ed;"
							v-for="item in selectNewsList">
							<view class="left" style="width: 460rpx;height: fit-content;">
								<view class="top" style="display: flex;flex-direction: column;height: 168rpx;">
									<text class="title" style="font-size: 32rpx;font-weight: bold;line-height: 1.5;display: -webkit-box;
												-webkit-box-orient: vertical;-webkit-line-clamp: 2;overflow: hidden;color: #303133;">{{item.title}}</text>
									<rich-text class="desc" style="display: -webkit-box;-webkit-box-orient: vertical;
									-webkit-line-clamp: 2;overflow: hidden;font-size: 26rpx;" :nodes="item.content"></rich-text>
								</view>
								<view class="bar" style="display: flex;flex-direction: row;justify-content: start;font-size: 22rpx;">
									<text style="color: #909399;">{{item.commentNum}} 条评论</text>
									<text style="margin-left: 20rpx;color: #909399;">{{item.publishDate}}</text>
								</view>
							</view>
							<view class="right"
								style="display: flex;justify-content: center;align-items: center;">
								<navigator :url="`/pages/news-detail/news-detail?id=${item.id}`">
									<image :src="$baseUrl + item.cover" mode="" style="width: 250rpx;height: 200rpx;"></image>
								</navigator>
							</view>
						</view>
						<u-loadmore :status="status" />
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				swiperList: [],
				swiperCurrent: 0,
				cateList: [],
				hotList: [],
				tabList: [],
				tabCurrent: 0,
				newsList: [],
				flag: false,
				selectList: [],
				status: 'nomore',
				value: ''
			}
		},
		onLoad() {
			if(!uni.getStorageSync('token')){
				uni.reLaunch({
					url:'/pages/login/login'
				})
			}
			uni.request({
				url: this.$baseUrl + '/prod-api/api/rotation/list',
				success: (res) => {
					console.log(res)
					this.swiperList = res.data.rows.map(item => ({
						image: this.$baseUrl + item.advImg,
						id: item.targetId
					}))
				}
			})
			uni.request({
				url: this.$baseUrl + '/prod-api/api/service/list',
				success: (res) => {
					console.log(res)
					this.cateList = res.data.rows.map(item => ({
						img: this.$baseUrl + item.imgUrl,
						name: item.serviceName,
						id: item.id
					})).splice(0, 9)
					console.log(this.cateList)
				}
			})
			uni.request({
				url: this.$baseUrl + '/prod-api/press/press/list',
				data: {
					hot: 'Y'
				},
				success: (res) => {
					console.log(res)
					this.hotList = res.data.rows
					console.log(this.hotList)
				}
			})
			uni.request({
				url: this.$baseUrl + '/prod-api/press/category/list',
				success: (res) => {
					console.log(res.data)
					this.tabList = res.data.data.map(item => ({
						name: item.name,
						id: item.id
					}))
					console.log(this.tabList)
				}
			})
			uni.request({
				url:this.$baseUrl + '/prod-api/press/press/list',
				success: (res) => {
					console.log(res.data)
					this.newsList = res.data.rows
					// this.getSelectList()
					this.flag = true
				}
			})
		},
		methods: {
			to(index) {
				this.swiperCurrent = index
				uni.navigateTo({
					url: `/pages/news-detail/news-detail?id=${this.swiperList[this.swiperCurrent].id}`
				})
			},
			tabChange(index) {
				this.tabCurrent = index
				// this.getSelectList()
			},
			submit(){
				uni.navigateTo({
					url:`/pages/search/search?key=${this.value}`
				})
			}
			// getSelectList(){
			// 	this.selectList = this.newsList.filter(item => item.type === this.tabList[this.tabCurrent].id.toString())
			// 	console.log(this.selectList)
			// }
		},
		computed: {
			selectNewsList(){
				return this.newsList.filter(item => item.type === this.tabList[this.tabCurrent].id.toString())
			}
		}
	}
</script>

<style lang="scss" scoped>
	page {
		height: 100%;

		view {
			height: 100%;
			display: flex;
			flex-direction: column;

			.main {
				flex: 1;
				display: flex;
				flex-direction: column;

				.content {
					height: fit-content;

					.cate {
						display: flex;
						flex-direction: row;
						flex-wrap: wrap;
						justify-content: space-between;
						padding: 20rpx;

						.cate-item {
							display: flex;
							flex-direction: column;
							align-items: center;
							justify-content: center;
							width: 120rpx;
							height: fit-content;
							margin-top: 20rpx;

							.icon {
								width: 100rpx;
								height: 100rpx;
							}

							.name {
								white-space: nowrap;
								width: 120rpx;
								overflow: hidden;
								text-overflow: ellipsis;
								text-align: center;
								margin-top: 10rpx;
								color: #606266;
								font-size: 26rpx;
							}
						}
					}
				}
			}
		}
	}
</style>